<template>
	<div  class="container">
		<el-page-header @back="goBack" content="部门设置"></el-page-header>

		<el-row type="flex" justify="space-between" class="nav-bar">
			<el-col :span="3">
				<el-button type="primary" @click="create"  v-show="utils.checkAdminPermission('系统设置.部门设置.新建')">新建</el-button>
			</el-col>
			<el-col class="text-right">
				<el-form :inline="true" label-width="auto" class="nav-bar-form">
					<el-form-item>
						<el-input placeholder="关键词搜索" v-model="key" clearable prefix-icon="el-icon-search" @clear="getList"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="getList">查询</el-button>
					</el-form-item>
				</el-form>
			</el-col>
		</el-row>

		<el-table :data="tableData" style="width: 100%" stripe>
			<el-table-column fixed prop="name" label="部门名称">
			</el-table-column>
			<el-table-column prop="contact" label="联系人" width="150">
			</el-table-column>
			<el-table-column prop="telephone" label="联系电话" width="150">
			</el-table-column>
			<el-table-column prop="sort" label="排序" width="100">
			</el-table-column>
			<el-table-column prop="status" label="状态" width="200">
				<template slot-scope="scope">
					<span class="color-success" v-if="scope.row.status">已启用</span>
					<span class="color-danger" v-else>未启用</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" fixed="right" width="180">
				<template slot-scope="scope">
					<el-button @click="edit(scope.row)" type="text" size="small"  v-show="utils.checkAdminPermission('系统设置.部门设置.编辑')">编辑</el-button>
					<!-- <el-button @click="create(scope.row)" type="text" size="small">修改</el-button> -->
					<el-button @click="del(scope.row.id)" type="text" size="small" v-show="utils.checkAdminPermission('系统设置.部门设置.删除')">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="table-pagination">
			<el-pagination background layout="sizes, total, prev, pager, next" :total="total"
				:page-sizes="[10, 50, 100, 200]" :page-size="pageSize" :current-page="page" @current-change="pageChange"
				@size-change="handleSizeChange">
			</el-pagination>
		</div>
		<el-dialog :title="dialogType=='add'?'新建部门':'编辑部门'" :visible.sync="dialogVisible" width="500px">
			<el-form ref="form" :model="form" label-width="100px">
				<el-form-item label="部门名称">
					<el-input v-model="form.name"></el-input>
				</el-form-item>
				<el-form-item label="排序">
					<el-input v-model="form.sort"></el-input>
				</el-form-item>
				<el-form-item label="部门联系人">
					<el-input v-model="form.contact"></el-input>
				</el-form-item>
				<el-form-item label="联系人电话">
					<el-input v-model="form.telephone"></el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-switch v-model="form.status"></el-switch>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="dialogVisible = false">取消</el-button>
				<el-button type="primary" @click="submitType">确定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		layout: 'admin',
    head() {
      return {
          title:'部门设置-' + this.websetting.name
      }
    },
		data() {
			return {
				key: '',
				page: 1,
				pageSize: 10,
				loading: false,
				total: 0,
				dialogVisible: false,
				dialogType: 'add',
				form: {
					name: '',
					sort: 0,
					status: true,
					contact: '',
					telephone: ''
				},
				tableData: []
			}
		},
		mounted() {
			this.getList()
		},
		methods: {
			goBack() {
				this.$router.go(-1)
			},
			getList() {
				this.$axios.post('/department/list', {
					"PageIndex": this.page,
					"PageSize": this.pageSize,
					"Key": this.key
				}).then((res) => {
					this.tableData = res.rows;
					this.total = res.total
				})
			},
			submitType() {
				let api = ''
				if (this.dialogType == 'add') {
					api = '/department/create'
				}
				if (this.dialogType == 'edit') {
					api = '/department/edit'
				}

				this.$axios.post(api, this.form).then((res) => {
					this.$message.success(res.msg);
					this.dialogVisible = false;
					this.getList()
				})
			},
			create() {
				this.dialogType = 'add'
				this.dialogVisible = true
				this.form = {
					name: '',
					sort: 0,
					status: true,
					contact: '',
					telephone: ''
				}
			},
			edit(row) {
				this.dialogType = 'edit'
				this.dialogVisible = true
				this.form.id = row.id
				this.form.name = row.name
				this.form.sort = row.sort
				this.form.status = row.status
				this.form.contact = row.contact
				this.form.telephone = row.telephone
			},
			search() {
				this.getList()
			},
			pageChange(e) { // 页码切换事件
				this.page = e;
				this.getList()
			},
			handleSizeChange(val) {
				this.page = 1;
				this.pageSize = val;
				this.getList()
			},
			del(id) {
				this.$confirm('此操作将删除该分类, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$axios.post('/department/delete?Ids='+id).then((res) => {
						this.$message.success(res.msg);
						this.getList()
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			}
		}
	}
</script>

<style lang="scss">
	.nav-bar {
		padding: 45px 0 0;
	}

	.send {
		margin-top: 15px;
		margin-right: 10px;
	}

	.dialogForm {
		display: flex;
		flex-wrap: wrap;

		.el-form-item {
			width: 50%;
			float: left;

			&.all {
				width: 100%;
			}

			.el-select {
				width: 100%;
			}
		}
	}
</style>
